import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import { DNavBar, DImagesupload, DAtInpute } from '@/components'
import http from '@/api'
import sdk from '@/common/sdk'
import './mycompany.scss'

export default class MycompanyBox extends Component {

    constructor(props) {
        super(props)
        this.state = {
            listInfo: [
                {
                    label: '所在企业',
                    value: '---'
                },
                {
                    label: '所在区域',
                    value: '---'
                },
                {
                    label: '企业地址',
                    value: "---"
                },
                {
                    label: '企业性质',
                    value: '---'
                },

            ],
            companyurl: [],
            companyintro: '',
            knowUrl: [],
            status: false,
            statusClk: false,
            id: '',
            title: '个人信息',
            page:1
        }
    }

    componentWillMount() {
        let creatorId = ''
        if (localStorage.getItem('userInformation')) {
            let { infoMyEntity = '' } = JSON.parse(localStorage.getItem('userInformation'))
            creatorId = infoMyEntity.imIdStatus
        }
		// creatorId = '1210110786093981905'
        let { id } = this.$router.params
        this.setState({
            id,
            creatorId,
            statusClk: creatorId === '1' ? true : false
        })
        this.getCompanyInfo(id)
    }

    componentDidShow() {
        let {title,page,id} = this.state
        sdk.setTitleInfo(title)
        if(page > 1){
        this.getCompanyInfo(id)
        }
    }

    getCompanyInfo(id) {
        http.getEntinfo({ id }).then(res => {
            if (!res.fileList) {
                return
            }
            let { companyurl, listInfo, knowUrl, companyintro,page } = this.state
            companyurl = []
            knowUrl = []
            companyintro = res.entInfo
            sessionStorage.setItem('companyintro', JSON.stringify(companyintro))
            companyurl.push({
                url: res.entPhotoUrl
            })
            res.fileList.map(item => {
                knowUrl.push({
                    name: item.cfFileName,
                    url: item.cfFileUrl,
                    type: item.cfType
                })
            })
            let _obj = ['entName', 'entDis', 'entAddr', 'entAtt']
            _obj.map((item, i) => {
                listInfo[i].value = res[item]
            })
            page++
            this.setState({
                companyurl,
                listInfo,
                knowUrl,
                companyintro,
                TXurl: res.entPhotoUrl,
                status: true,
                page
            })
        })
    }

    onRightHanlde() {
        let { creatorId } = this.state
        if (creatorId === '2') {
            return Taro.showToast({
                title: '企业员工无法查看',
                icon: 'none'
            })
        }
        const { knowUrl } = this.state
        sessionStorage.setItem('knowurl', JSON.stringify(knowUrl))
        Taro.navigateTo({
            url: '/pages/knowimages/knowimages'
        })
    }

    bindCarform(key, value) {
        if (key) {
            let { companyurl } = this.state
            companyurl = []
            companyurl = [...value]
            this.setState({
                companyurl
            }, () => {
                this.getUploadCompany()
            })
        }
    }

    getUploadCompany() {
        let { id, companyurl } = this.state
        if (companyurl.length === 0) {
            return
        }
        let _obj = {
            entId: id,
            fileId: companyurl[0].companyurl,
            url: companyurl[0].url
        }
        http.uploadCompany(_obj).then(() => {
            if (!res) {
                return
            }
            Taro.showToast({
                title: '企业头像已更换!',
                duration: 800
            })
        })
    }
    handleEdit() {
        let { creatorId } = this.state
        if (creatorId === '2') {
            return Taro.showToast({
                title: '企业员工无法进行编辑',
                icon: 'none'
            })
        }
        Taro.navigateTo({
            url: `/pages/mycompanyintro/mycompanyintro`
        })
    }
    render() {
        const { listInfo, companyurl, status, companyintro, statusClk, title } = this.state
        const _imageList = function (key, value) {
            let _value = []
            value.map(item => {
                if (item.url) {
                    _value.push({
                        fileId: item.id,
                        type: item.uploadType,
                        url: item.url
                    })
                }
            })
            this.bindCarform(key, _value)
        }
        return (
            <View className="b_mycompany">
                <DNavBar title={title}></DNavBar>
                {
                    status && <View className="b_mycompany__box">
                        <View className="b_mycompany__info">
                            <View className="item_title item_box">
                                <Text className="item_title__left">企业照片</Text>
                                <View className="item_title__right_img">
                                    <DImagesupload className='b_mycompany__img'
                                        value={companyurl} count={1}
                                        statusClk={statusClk}
                                        bind='imageUrl' onChangeImages={_imageList.bind(this, 'imageUrl')}
                                    ></DImagesupload>
                                </View>
                            </View>
                            {
                                listInfo && listInfo.map((item, i) => {
                                    return <View className="item_title" key={i}>
                                        <Text className="item_title__left">{item.label}</Text>
                                        <Text className="item_title__right">{item.value}</Text>
                                    </View>
                                })
                            }
                            <View className='b_mycompany__know'>
                                <DAtInpute title='企业简介' icon='chevron-right' placeholder='请输入您的企业简介' onRightHanlde={this.handleEdit.bind(this)}></DAtInpute>
                            </View>
                            <View className='b_mycompany__know'>
                                <DAtInpute title='资料图片' icon='chevron-right' onRightHanlde={this.onRightHanlde.bind(this)}></DAtInpute>
                            </View>
                            {/* <View className='b_mycompany__footer'>
                                <AtButton className='item_btn' onClick={this.onClickHandle.bind(this)}>重新认证</AtButton>
                            </View> */}
                        </View>
                    </View>
                }
            </View>
        )
    }
}
